package com.webserver.core;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WebServerApplication {
    private ServerSocket serverSocket;
    private ExecutorService threadPool;
    public WebServerApplication(){
        try {
            System.out.println("正在启动服务端");
            serverSocket=new ServerSocket(8088);
            threadPool= Executors.newFixedThreadPool(50);
            System.out.println("服务端启动成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {
            while (true){
                System.out.println("等待客户端连接...");
                Socket socket=serverSocket.accept();
                System.out.println("一个客户端连接成功了!");

                ClientHandler handler=new ClientHandler(socket);
                /*Thread thread=new Thread(handler);
                thread.start();*/
                threadPool.execute(handler);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        WebServerApplication application=new WebServerApplication();
        application.start();
    }

}
